package com.example.mail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import com.example.ISelectable;
import com.example.MessageCodes;
import com.example.R;
import com.example.popup.Select;
import core.model.Game;
import core.model.mail.Message;
import core.util.Logger;

import java.util.ArrayList;

import static core.model.mail.Message.MessageType.BROADCAST;
import static core.model.mail.Message.MessageType.PRIVATE;


public class MailTypeActivity extends Activity implements ISelectable {

    private MailManager mailManager;
    private Game game;

    private CharSequence[] messageTypes = new CharSequence[]{"Broadcasts", "Privates"};

    // todo should be enum or const
    private String selectedMailType;

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MessageCodes.Mail.LOADING_MESSAGES_COMPLETE:
                    Logger.debug("MailTypeActivity", "Mail loading was complete without any problems.");
                    showUI();
                    break;
                case MessageCodes.EXCEPTION:
                    Logger.debug("MailTypeActivity", "Mail loading was not complete.");
                    break;
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        game = (Game) getIntent().getExtras().getSerializable("game");

        Select.messageType(messageTypes, this, this);
    }


    private void loadMessages() {
        mailManager = new MailManager(this, game, handler);
        mailManager.loadMessages();
    }

    private void showUI() {
        setContentView(R.layout.mailtypelayout);

        final core.model.mail.Context context = mailManager.getContext();

        if ("Privates".equalsIgnoreCase(selectedMailType)) {
                selectedMailType = "private";
                showMessageList(PRIVATE, context);
        }

        if ("Broadcasts".equalsIgnoreCase(selectedMailType)) {
                selectedMailType = "broadcast";
                showMessageList(BROADCAST, context);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        Select.messageType(messageTypes, this, this);
    }

    private void showMessageList(Message.MessageType messageType, core.model.mail.Context context) {
        View view = findViewById(R.id.mailTypes);
        Context viewContext = view.getContext();

        Intent i = new Intent(viewContext, MailListActivity.class);

        switch (messageType) {
            case PRIVATE:
                // todo "messages" should constant
                i.putExtra("messages", (ArrayList<Message>) context.getPrivates());
                break;

            case BROADCAST:
                // todo "messages" should constant
                i.putExtra("messages", (ArrayList<Message>) context.getBroadcasts());
                break;
        }

        i.putExtra("game", game);
        i.putExtra("selectedMailType", selectedMailType);

        viewContext.startActivity(i);
    }

    @Override
    public void onItemSelected(int type, int index) {
        if (Select.TYPE_MESSAGE_TYPE == type) {
            selectedMailType = messageTypes[index].toString();
            loadMessages();
        }

    }
}
